import redis

class RedisUtil:
    def __init__(self, host='localhost', port=6379, db=0, password=None):
        """初始化Redis连接"""
        self.client = redis.Redis(host=host, port=port, db=db, password=password)
    
    def isConnection(self):
        self.client.ping()

    def set(self, key, value, ex=None, px=None, nx=False, xx=False):
        """设置键值对"""
        if ex:
            return self.client.set(key, value, ex=ex, px=px, nx=nx, xx=xx)
        else:
            return self.client.set(key, value)

    def get(self, key):
        """获取键对应的值"""
        return self.client.get(key)

    def delete(self, key):
        """删除键"""
        return self.client.delete(key)

    def exists(self, key):
        """检查键是否存在"""
        return self.client.exists(key)

    def hset(self, key, field, value):
        """设置哈希表的字段值"""
        return self.client.hset(key, field, value)

    def hget(self, key, field):
        """获取哈希表的字段值"""
        return self.client.hget(key, field)

    def hdel(self, key, field):
        """删除哈希表的字段"""
        return self.client.hdel(key, field)